package cn.dengta.webapp.trade.form;

import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.validation.constraints.NotNull;

import cn.dengta.common.context.I18N;
import cn.dengta.common.validator.NotEmpty;
import cn.dengta.webapp.base.form.BaseForm;
import cn.dengta.webapp.trade.entity.FormTemplate;
import cn.dengta.webapp.trade.model.PaymentMethod;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.Assert;
import me.codeplayer.util.StringUtil;
import org.springframework.cglib.beans.BeanMap;

@Getter
@Setter
public class BindBankCardForm extends BaseForm<BindBankCardForm> {

	/** 国家 */
	@NotEmpty
	private String country;
	/**
	 * 表单类型：1=绑定银行卡；2=绑定钱包；3=支付宝
	 *
	 * @see PaymentMethod
	 */
	@NotNull
	private Integer formType;
	/** 子类型 @see CashSubTypeMethod */
	private Integer subType;
	private String firstName;
	private String middleName;
	private String lastName;
	/** 银行账号 */
	private String account;
	/** 银行机构 */
	private String bankCode;
	/** IFSC code； */
	private String ifscCode;
	/** 地址 */
	private String address;
	/** 邮箱 */
	private String email;
	/** 手机号 */
	private String phone;

	private String certNo;

	private String captcha;
	private String zipCode;
	private String addrCity;
	private String addrState;
	private String accType;
	private String docType;
	private String docId;
	private String bankBranch;
	private String checkDigit;



	public void setFirstName(String firstName) {
		if (StringUtil.notEmpty(firstName)) {
			this.firstName = firstName.trim();
		}
	}

	public void setMiddleName(String middleName) {
		if (StringUtil.notEmpty(middleName)) {
			this.middleName = middleName.trim();
		}
	}

	public void setLastName(String lastName) {
		if (StringUtil.notEmpty(lastName)) {
			this.lastName = lastName.trim();
		}
	}

	@Override
	public void validate() {
		super.validate();
		if (formType == 3) {
			Assert.notNull(firstName, "姓名不能为空");
			Assert.notNull(account, "支付宝账号不能为空");
		}
	}

	@SuppressWarnings("unchecked")
	public static void validate(BindBankCardForm form, Map<String, FormTemplate> formTemplateMap) {
		Map<String, Object> formMap = BeanMap.create(form);
		for (Map.Entry<String, FormTemplate> entry : formTemplateMap.entrySet()) {
			FormTemplate formTemplate = entry.getValue();
			if (1 == formTemplate.getRequired()) {
				// 必填
				Assert.notNull(formMap.get(formTemplate.getName()), () -> I18N.msg(formTemplate.getErrorMsg()));
			}

			if (formMap.get(formTemplate.getName()) != null) {
				if ("options".equals(formTemplate.getName())) {
					// 银行机构 下拉框选择
					/*ist<Option<Object>> options = formTemplate.getOptions();
					Optional<CashBank> first = options.stream().filter(r -> form.getBankCode().equals(r.getBankCode())).findFirst();
					I18N.assertTrue(first.isPresent(), formTemplate.getErrorMsg());*/
				} else {
					// 正则表达式判断
					if(StringUtil.notEmpty(formTemplate.getRegex()) && StringUtil.notEmpty(formMap.get(entry.getKey()).toString())) {
						Pattern pattern = Pattern.compile(formTemplate.getRegex());
						Matcher matcher = pattern.matcher(formMap.get(entry.getKey()).toString());
						if (!matcher.matches()) {
							throw new IllegalArgumentException(I18N.msg(formTemplate.getErrorMsg()));
						}
					}
				}
			}
		}
	}

}
